package com.hoshiicloud.saleorder.service.impl.customer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.saleorder.feign.IdGeneratorService;
import com.hoshiicloud.saleorder.mapper.customer.ReviewsImagesMapper;
import com.hoshiicloud.saleorder.service.customer.IReviewsImagesService;
import com.hoshiicloud.user.entity.customer.ReviewsImages;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 平台商品评价-图片及视频 服务实现类
 * </p>
 *
 * @author Mr.Wu
 * @since 2019-04-28
 */
@Service
public class ReviewsImagesServiceImpl extends ServiceImpl<ReviewsImagesMapper, ReviewsImages>
        implements IReviewsImagesService {

    @Autowired
    private ReviewsImagesMapper reviewsImagesMapper;

    @Autowired
    private IdGeneratorService idGeneratorService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = {"ReviewsImages"}, allEntries = true)
    @Logable(authName = "前台用户操作-平台商品评价-图片及视频信息管理-新增平台商品评价-图片及视频信息")
    public Response insert(ReviewsImages param) {
        String id = idGeneratorService.queryGeneratorId();
        // 传入路径信息
        // 上传文件时调用内容审核接口
        if (StringUtils.isBlank(id)) {
            throw new ServiceException(CommonError.error("id服务失败"));
        }
        param.setId(Long.parseLong(id));
        int count = reviewsImagesMapper.insert(param);
        if (count <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return Response.success("新增成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = {"ReviewsImages"}, allEntries = true)
    @Logable(authName = "前台用户操作-平台商品评价-图片及视频信息管理-修改平台商品评价-图片及视频信息")
    public Response updateDataById(ReviewsImages param) {
        // 传入路径信息
        // 上传文件时调用内容审核接口
        int count = reviewsImagesMapper.updateById(param);
        if (count <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return Response.success("修改成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = {"ReviewsImages"}, allEntries = true)
    @Logable(authName = "前台用户操作-平台商品评价-图片及视频信息管理-删除平台商品评价-图片及视频信息")
    public Response deleteDataById(String id) {
        int count = reviewsImagesMapper.deleteById(id);
        if (count <= 0) {
            throw new ServiceException(CommonError.error("删除失败"));
        } else {
            return Response.success("删除成功");
        }
    }

    @Override
    @Cacheable(cacheNames = "ReviewsImages", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "前台用户操作-平台商品评价-图片及视频信息管理-查询平台商品评价-图片及视频信息详情")
    public Response getDetail(String id) {
        ReviewsImages data = reviewsImagesMapper.selectById(id);
        if (data == null) {
            return Response.failed("数据不存在");
        } else {
            return Response.data(data);
        }
    }

    @Override
    @Cacheable(cacheNames = "ReviewsImages", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+T(com.hoshiicloud.common.utils.DigestUtils).md5(#key)")
    @Logable(authName = "前台用户操作-平台商品评价-图片及视频信息管理-查询平台商品评价-图片及视频信息列表")
    public Response getList(QueryWrapper<ReviewsImages> queryWrapper, String key) {
        return Response.data(reviewsImagesMapper.selectList(queryWrapper));
    }

    @Override
    @Cacheable(cacheNames = "ReviewsImages", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+T(com.hoshiicloud.common.utils.DigestUtils).md5(#key)")
    @Logable(authName = "前台用户操作-平台商品评价-图片及视频信息管理-分页查询平台商品评价-图片及视频信息")
    public Response getPage(IPage<ReviewsImages> page, QueryWrapper<ReviewsImages> queryWrapper, String key) {
        return Response.data(reviewsImagesMapper.selectPage(page, queryWrapper));
    }
}
